package com.lanqiao.attendance.config;


import com.lanqiao.attendance.common.LoginInterceptor;
import com.lanqiao.attendance.common.PermissionInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMVCConfig implements WebMvcConfigurer {
    @Autowired
    private LoginInterceptor loginInterceptor;
    @Autowired
    private PermissionInterceptor permissionInterceptor;

    @Value("${uploadFile.path}")
    String path;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
//        //跨域配置,这里主要是配置前后端跨域
        registry.addMapping("/**").allowedOrigins("*");
    }


    /**
     * 配置静态资源访问，比如图片
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("file:d:/img/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //登录拦截器器，优先级1
        registry.addInterceptor(loginInterceptor).order(1)
//              全局用户
                .addPathPatterns("/user/**")
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/user/register")
//                学生,老师，管理员
//                .addPathPatterns("/student/**")
//                .addPathPatterns("/teacher/**")
//                .addPathPatterns("/class/**")

        ;

//        // 权限拦截器优先级2
//        registry.addInterceptor(permissionInterceptor).order(2)
//                ;


    }
}
